﻿/***********************************************************************
 * <copyright file="SharedController.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Wednesday, March 12, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Web.Mvc;
using DxMvcApplication.Areas.Administrator.Models;
using DxMvcApplication.Repositories.Core.Security;
using DxMvcApplication.Repositories.Implementations.Security;

namespace DxMvcApplication.Areas.Administrator.Controllers.Security
{
    public class SharedController : Controller
    {
        //
        // GET: /Administrators/Shared/
        private readonly ISiteRepository _siteRepository;
        private readonly IPermissionRepository _permissionRepository;
        private static int _userId;
        public SharedController()
        {
            _siteRepository = new SiteRepository();
            _permissionRepository = new PermissionRepository();
        }

        //[OutputCache(Duration = 3600)]
        /// <summary>
        /// Tools the bar.
        /// </summary>
        /// <returns></returns>
        public ActionResult ToolBar()
        {
            int.TryParse(Session["UserId"].ToString(), out _userId);
            var result = _siteRepository.GetList(_userId, 0);
            return PartialView(result.ToModel());
        }

        //[OutputCache(Duration = 3600)]
        /// <summary>
        /// Navigations the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public PartialViewResult Navigation(int id)
        {
            var result = _siteRepository.GetList(_userId, id);
            return PartialView(result.ToModel());
        }

        /// <summary>
        /// Subs the navigation.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public PartialViewResult SubNavigation(int id)
        {
            var result = _siteRepository.GetList(_userId, id);
            return result.Count > 0 ? PartialView(result.ToModel()) : null;
        }

        /// <summary>
        /// Commands this instance.
        /// </summary>
        /// <returns></returns>
        public PartialViewResult Command()
        {
            var siteId = 0;
            var siteIdParam = RouteData.Values["siteId"];
            if (siteIdParam != null)
            {
                int.TryParse(siteIdParam.ToString(), out siteId);
            }
            ViewBag.Permissions = _permissionRepository.GetListByUserAndSite(_userId, siteId).ToModel();
            return PartialView();
        }
    }
}
